function loadtoc(e) {
    function t() {
        if ("entry" in e.feed) {
            var t = e.feed.entry.length;
            totalEntires = totalEntires + t;
            totalPosts = e.feed.openSearch$totalResults.$t;
            if (totalPosts > totalEntires) {
                var n = document.createElement("script");
                n.type = "text/javascript";
                startindex = totalEntires + 1;
                n.setAttribute("src", "/feeds/posts/summary?start-index=" + startindex + "&max-results=500&alt=json-in-script&callback=loadtoc");
                tocdiv.appendChild(n)
            }
            for (var r = 0; r < t; r++) {
                var i = e.feed.entry[r];
                var s = i.title.$t;
                var o = i.published.$t.substring(0, 10);
                var u;
                for (var a = 0; a < i.link.length; a++) {
                    if (i.link[a].rel == "alternate") {
                        u = i.link[a].href;
                        break
                    }
                }
                if ("content" in i) {
                    var f = i.content.$t
                } else if ("summary" in i) {
                    var f = i.summary.$t
                } else var f = "";
                var l = /<\S[^>]*>/g;
                f = f.replace(l, "");
                if (f.length > numChars) {
                    f = f.substring(0, numChars);
                    var c = f.lastIndexOf(" ");
                    f = f.substring(0, c) + "..."
                }
                var h = "";
                if ("category" in i) {
                    for (var a = 0; a < i.category.length; a++) {
                        h += "<a href=\"javascript:filterPosts('" + i.category[a].term + "');\" title=\"Click here to select all posts with label '" + i.category[a].term + "'\">" + i.category[a].term + "</a>,  "
                    }
                    var p = h.lastIndexOf(",");
                    if (p != -1) {
                        h = h.substring(0, p)
                    }
                }
                postTitle.push(s);
                postDate.push(o);
                postUrl.push(u);
                postSum.push(f);
                postLabels.push(h)
            }
        }
        if (totalEntires == totalPosts) {
            tocLoaded = true;
            showToc()
        }
    }
    t();
    sortPosts(sortBy);
    tocLoaded = true
}

function filterPosts(e) {
    postFilter = e;
    displayToc(postFilter)
}

function allPosts() {
    postFilter = "";
    displayToc(postFilter)
}

function sortPosts(e) {
    function t(e, t) {
        var n = postTitle[e];
        postTitle[e] = postTitle[t];
        postTitle[t] = n;
        var n = postDate[e];
        postDate[e] = postDate[t];
        postDate[t] = n;
        var n = postUrl[e];
        postUrl[e] = postUrl[t];
        postUrl[t] = n;
        var n = postSum[e];
        postSum[e] = postSum[t];
        postSum[t] = n;
        var n = postLabels[e];
        postLabels[e] = postLabels[t];
        postLabels[t] = n
    }
    for (var n = 0; n < postTitle.length - 1; n++) {
        for (var r = n + 1; r < postTitle.length; r++) {
            if (e == "titleasc") {
                if (postTitle[n] > postTitle[r]) {
                    t(n, r)
                }
            }
            if (e == "titledesc") {
                if (postTitle[n] < postTitle[r]) {
                    t(n, r)
                }
            }
            if (e == "dateoldest") {
                if (postDate[n] > postDate[r]) {
                    t(n, r)
                }
            }
            if (e == "datenewest") {
                if (postDate[n] < postDate[r]) {
                    t(n, r)
                }
            }
        }
    }
}

function displayToc(e) {
    var t = 0;
    var n = "";
    var r = "Lowongan Kerja";
    var i = "Klik untuk mengurutkan berdasarkan judul";
    var s = "Tanggal";
    var o = "Klik untuk mengurutkan berdasarkan tanggal";
    var u = "Kategori";
    var a = "";
    if (sortBy == "titleasc") {
        i += " (descending)";
        o += " (newest first)"
    }
    if (sortBy == "titledesc") {
        i += " (ascending)";
        o += " (newest first)"
    }
    if (sortBy == "dateoldest") {
        i += " (ascending)";
        o += " (newest first)"
    }
    if (sortBy == "datenewest") {
        i += " (ascending)";
        o += " (oldest first)"
    }
    if (postFilter != "") {
        a = "Klik untuk menampilkan semua posting"
    }
    n += '<table id="box-table-a">';
    n += "<tr>";
    n += '<th>';
    n += '<a href="javascript:toggleTitleSort();" title="' + i + '">' + r + "</a>";
    n += "</th>";
    n += '<th>';
    n += '<a href="javascript:toggleDateSort();" title="' + o + '">' + s + "</a>";
    n += "</th>";
    n += '<th>';
    n += '<a href="javascript:allPosts();" title="' + a + '">' + u + "</a>";
    n += "</th>";
    n += "</tr>";
    for (var f = 0; f < postTitle.length; f++) {
        if (e == "") {
            n += '<tr><td class="toc-entry-col1"><a href="' + postUrl[f] + '" title="' + postSum[f] + '">' + postTitle[f] + '</a></td><td class="toc-entry-col2">' + postDate[f] + '</td><td class="toc-entry-col3">' + postLabels[f] + "</td></tr>";
            t++
        } else {
            z = postLabels[f].lastIndexOf(e);
            if (z != -1) {
                n += '<tr><td class="toc-entry-col1"><a href="' + postUrl[f] + '" title="' + postSum[f] + '">' + postTitle[f] + '</a></td><td class="toc-entry-col2">' + postDate[f] + '</td><td class="toc-entry-col3">' + postLabels[f] + "</td></tr>";
                t++
            }
        }
    }
    n += "</table>";
    tocdiv.innerHTML = n
}

function toggleTitleSort() {
    if (sortBy == "titleasc") {
        sortBy = "titledesc"
    } else {
        sortBy = "titleasc"
    }
    sortPosts(sortBy);
    displayToc(postFilter)
}

function toggleDateSort() {
    if (sortBy == "datenewest") {
        sortBy = "dateoldest"
    } else {
        sortBy = "datenewest"
    }
    sortPosts(sortBy);
    displayToc(postFilter)
}

function showToc() {
    if (tocLoaded) {
        displayToc(postFilter);
        var e = document.getElementById("toclink")
    } else {
        alert("Loading...")
    }
}

function hideToc() {
    var e = document.getElementById("toc");
    e.innerHTML = "";
    var t = document.getElementById("toclink");
    t.innerHTML = '<a href="#" onclick="scroll(0,0); showToc(); Effect.toggle(' + "'toc-result','blind');" + '">» Show Table of Contents</a> <img src="http://chenkaie.blog.googlepages.com/new_1.gif"/>'
}
var postTitle = new Array;
var postUrl = new Array;
var postDate = new Array;
var postSum = new Array;
var postLabels = new Array;
var sortBy = "datenewest";
var tocLoaded = false;
var numChars = 250;
var postFilter = "";
var tocdiv = document.getElementById("bp_toc");
var totalEntires = 0;
var totalPosts = 0;